//
//  ProductImageTableViewCell.swift
//  koolcar
//
//  Created by kuaimaxiansheng on 2023/10/26.
//

import UIKit
import JXPhotoBrowser
import FSPagerView
class ProductImageTableViewCell: UITableViewCell {
    var pagerView: FSPagerView!
    var pageControl: TYPageControl!
    var dataArr: [String] = []//图片
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        self.selectionStyle = .none
        self.backgroundColor = .white
        self.clipsToBounds = true
        self.initUI()
    }
    
    func initUI(){
        
        // Create a pager view
        self.pagerView = FSPagerView(frame: CGRect(x: 0, y: 10, width: kScreenWidth, height: 280))
        self.pagerView.dataSource = self
        self.pagerView.delegate = self
        self.pagerView.decelerationDistance = 1
        let width = (kScreenWidth)
        //        self.pagerView.automaticSlidingInterval = 3
        self.pagerView.interitemSpacing = 10
        self.pagerView.isInfinite = false//顺时针一直滑动
        self.pagerView.itemSize = CGSize(width: width, height: 280)
        self.pagerView.register(CarShowImageCell.self, forCellWithReuseIdentifier: "CarShowImageCell")
        self.contentView.addSubview(self.pagerView)
        
        
        
        self.pageControl = TYPageControl(frame: CGRect(x: 0, y: self.pagerView.frame.maxY + 6, width: kScreenWidth, height: 4))
        self.pageControl.currentPageIndicatorSize = CGSize(width: 6, height: 6)
        self.pageControl.pageIndicatorSize = CGSize(width: 6, height: 6)
        self.pageControl.backgroundColor = .clear
        self.pageControl.currentPage = 0
        self.pageControl.currentPageIndicatorTintColor = Theme.mainColor
        self.pageControl.pageIndicatorTintColor = UIColor(hex: 0x005C99,alpha: 0.1)//不活跃指示器颜色
        self.pagerView.addSubview(self.pageControl)
        
        
        self.pagerView.snp.makeConstraints { make in
            make.top.equalTo(0)
            make.left.equalTo(0)
            make.right.equalTo(0)
            make.height.equalTo(280)
            make.bottom.equalTo(0)
        }
        
        self.pageControl.snp.makeConstraints { make in
            make.centerX.equalTo(self.pageControl.snp.centerX)
            make.height.equalTo(10)
            make.left.equalTo(38)
            make.right.equalTo(-38)
            make.bottom.equalTo(-10)
        }
        
        
    }
    
    
    //model
    func setModel(model: ProductDetailDataModel){
       self.dataArr = (model.productImages ?? "").components(separatedBy: ",")
        if  self.dataArr.count > 1 {
            self.pageControl.numberOfPages = self.dataArr.count
        }else{
            self.pageControl.numberOfPages = 0
        }
        self.pagerView.reloadData()
        self.updateConstraints()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override func awakeFromNib() {
        super.awakeFromNib()
        // Initialization code
    }

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }

}

extension ProductImageTableViewCell: FSPagerViewDataSource,FSPagerViewDelegate{
    
     func numberOfItems(in pagerView: FSPagerView) -> Int {
         return self.dataArr.count
    
    }
    
     func pagerView(_ pagerView: FSPagerView, cellForItemAt index: Int) -> FSPagerViewCell {
        let cell = pagerView.dequeueReusableCell(withReuseIdentifier: "CarShowImageCell", at: index) as! CarShowImageCell
         cell.bgVrView.isHidden = true
         if self.dataArr.count > index {
             cell.bgImgeView.sd_setImage(with: URL.init(string: self.dataArr[index]), placeholderImage: UIImage(named: "icon_default_carS"))
         }
         return cell
    }
    
    func pagerView(_ pagerView: FSPagerView, didSelectItemAt index: Int) {
        pagerView.deselectItem(at: index, animated: true)
        pagerView.scrollToItem(at: index, animated: true)
        
      
        if self.dataArr.count > index{
            // 实例化
            let browser = JXPhotoBrowser()
            // 浏览过程中实时获取数据总量
            browser.numberOfItems = {self.dataArr.count}
            // 刷新Cell数据。本闭包将在Cell完成位置布局后调用。
            browser.reloadCellAtIndex = { context in
                let url = self.dataArr[context.index]
                let browserCell = context.cell as? JXPhotoBrowserImageCell
                browserCell?.index = context.index
                let collectionPath = IndexPath(item: context.index, section: 0)
                let collectionCell = pagerView.cellForItem(at: index) as? CarShowImageCell
                let placeholder = collectionCell?.bgImgeView.image
                // 用 SDWebImage 加载
                browserCell?.imageView.sd_setImage(with: URL.init(string: url), placeholderImage: placeholder)
            }
            browser.pageIndicator = JXPhotoBrowserNumberPageIndicator()
            browser.pageIndex = index
            browser.show()// 展示
        }
    }
    
 
    
    func pagerViewDidEndDecelerating(_ pagerView: FSPagerView){
        print("滑动到了第\(pagerView.currentIndex)个row")
        self.pageControl.currentPage = pagerView.currentIndex
    }
    
}
